package com.oedtech.esl.controller.eslController;

import com.oedtech.esl.controller.BaseController;
import com.oedtech.esl.model.esl.Ap;
import com.oedtech.esl.model.esl.EslImg;
import com.oedtech.esl.repository.esl.ApDao;
import com.oedtech.esl.repository.esl.EslImgDao;
import com.oedtech.esl.service.eslService.ApService;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author QiuLongYu
 * @Date 2019/7/26
 */
@RestController
@RequestMapping("/ap_rest")
public class ApController extends BaseController{

    @Autowired
    private ApDao apDao;

    @Autowired
    private ApService apService;




    @RequestMapping("/delete")
    public void deleteAp(String apid){
        apService.deleteAp(apid);
    }







    @RequestMapping("/ap/listByPage")
    @ResponseBody
    public Page<Ap> listByPage(int page, int size, String sortStr, boolean sortAscOrDesc, String searchObj, Principal principal)
    {

        Specification<Ap> specification = getSpecification(searchObj,principal.getName());

        if (StringUtils.isEmpty(sortStr))
        {
            return apDao.findAll(specification, PageRequest.of(page, size));
        }
        else
        {
            return apDao.findAll(specification, PageRequest.of(page, size, new Sort((sortAscOrDesc ? Sort.Direction.ASC : Sort.Direction.DESC), sortStr)));
        }
    }
    private Specification getSpecification(String searchObj,String username)
    {
        JSONObject search = (JSONObject) JSONValue.parse(searchObj);

        Specification<Ap> specification = new Specification<Ap>()
        {
            @Override
            public Predicate toPredicate(Root<Ap> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder)
            {
                List<Predicate> queryList = new ArrayList<Predicate>();

                if (search.get("apId") != null && !StringUtils.isEmpty(search.get("apId") + ""))
                {
                    queryList.add(criteriaBuilder.like(root.get("apId"), "%"+search.get("apId") + "%"));
                }
                if (search.get("mac") != null && !StringUtils.isEmpty(search.get("mac") + ""))
                {
                    queryList.add(criteriaBuilder.like(root.get("mac"), "%"+search.get("mac") + "%"));
                }

                if (queryList.size() > 0)
                {
                    criteriaQuery.where(criteriaBuilder.and(queryList.toArray(new Predicate[queryList.size()])));
                }

                return criteriaQuery.getRestriction();
            }
        };

        return specification;
    }

}
